jQuery.fn.extend({
    uploadPreview: function (opts) {
        var _self = this,
            _this = $(this);
        opts = jQuery.extend({
            Img: "ImgPr",
            Width: 100,
            Height: 100,
            ImgType: ["jpg", "png", "jpeg"],
            addWaterMark: true,
            Callback: function () {}
        }, opts || {});
        _self.getObjectURL = function (file) {
            var url = null;
            if (window.createObjectURL != undefined) {
                url = window.createObjectURL(file)
            } else if (window.URL != undefined) {
                url = window.URL.createObjectURL(file)
            } else if (window.webkitURL != undefined) {
                url = window.webkitURL.createObjectURL(file)
            }
            return url
        };
        _this.change(function () {
        	var width = window.screen.width;
        	var isPhone = false;
        	if (width < 601) {
        		isPhone = true;
        	}
            if (this.value) {
                if (!isPhone && !RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
                    layer.alert("选择文件错误,图片类型必须是" + opts.ImgType.join(",") + "中的一种");
                    this.value = "";
                    return false
                }
                if (!lessMaxSize(this.files[0])) {
                	layer.alert("证件照太大,请按照提示要求上传图片");
                	this.value = "";
                	return false;
                }
                if ($.support.msie) {
                    try {
                    	//是否添加水印
                    	if(opts.addWaterMark) {
                    		$("#" + opts.Img).parents("a").children(".tip").css("display","block");
                    	}
                    	$("#" + opts.Img).siblings("span").html("");
                        $("#" + opts.Img).attr('src', _self.getObjectURL(this.files[0]));
                    } catch (e) {
                        var src = "";
                        var obj = $("#" + opts.Img);
                        var div = obj.parent("div")[0];
                        _self.select();
                        if (top != self) {
                            window.parent.document.body.focus()
                        } else {
                            _self.blur()
                        }
                        src = document.selection.createRange().text;
                        document.selection.empty();
                        obj.hide();
                        obj.parent("div").css({
                            'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
                            'width': opts.Width + 'px',
                            'height': opts.Height + 'px',
                        });
                        div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src
                    }
                } else {
                	//是否添加水印
                	if(opts.addWaterMark) {
                		$("#" + opts.Img).parents("a").children(".tip").css("display","block");
                	}
                	$("#" + opts.Img).siblings("span").html("");
                    $("#" + opts.Img).prop('src', _self.getObjectURL(this.files[0]));
                }
                opts.Callback()
            }
        })
    }
});